import Home from '@/pages/home'
import router from '@/router'
import url = require('url')
import path from 'path'
import express from 'express'
import { Route, Routes } from 'react-router-dom'
import { renderToString } from 'react-dom/server'
import { StaticRouter } from 'react-router-dom/server'
import { Helmet } from 'react-helmet'
import bodyParser = require('body-parser')
import store from '@/store'

const app = express()
app.use(express.static(path.resolve(process.cwd(), 'client_build')))

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.post('/api/getDemoData', (req, res) => {
  console.log(22222222, store)

  res.send({
    data: req.body,
    status_code: 0,
  })
})

app.get('*', (req, res) => {
  const content = renderToString(
    <StaticRouter location={req.path}>
      <Routes>
        {router.map((item) => {
          return <Route {...item} />
        })}
      </Routes>
    </StaticRouter>
  )

  const helmet = Helmet.renderStatic()

  res.send(`
    <html>
      <head>
        ${helmet.title.toString()}
        ${helmet.meta.toString()}
      </head>
      <body>
          <div id="root">${content}</div>
          <script src="./index.js"></script>
      </body>
    </html>  
  `)
})

app.post('/api/home', (req, res) => {})

app.listen(3000, () => {
  console.log('-----Server started on port 3000------')
})
